package cn.tedu.csmall.product.Controller;

import cn.tedu.csmall.product.pojo.dto.CategoryAddNewDTO;
import cn.tedu.csmall.product.pojo.vo.CategoryListItemVO;
import cn.tedu.csmall.product.service.ICategoryService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liyang
 * @version 1.0
 * @package cn.tedu.csmall.product.Controller
 * @file CategoryController.java
 * @description
 * @time 2024/10/2 上午11:41
 */
@Slf4j
@RestController
@RequestMapping("/categories")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    @PostMapping("/add-new")
    public JsonResult<Void> addNew(CategoryAddNewDTO categoryAddNewDTO) {
        log.debug("xxx");
        categoryService.addNew(categoryAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/categories/9527/enable
    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperation("启用类别")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "类别ID", required = true, example = "9527", dataType = "long")
    public JsonResult<Void> setEnable(@Range(min = 1, max = 1000000, message = "启用类别失败，类别ID非法！")
                                      @PathVariable Long id) {
        log.debug("开始处理【启用类别】的请求，参数：{}", id);
        categoryService.setEnable(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/categories/9527/disable
    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperation("禁用类别")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParam(name = "id", value = "类别ID", required = true, example = "9527", dataType = "long")
    public JsonResult<Void> setDisable(@Range(min = 1, max = 1000000, message = "禁用类别失败，类别ID非法！")
                                       @PathVariable Long id) {
        log.debug("开始处理【禁用类别】的请求，参数：{}", id);
        categoryService.setDisable(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/categories/list-by-parent
    @GetMapping("/list-by-parent")
    @ApiOperation("根据父级查询子级类别列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<CategoryListItemVO>> listByParentId(Long parentId) {
        log.debug("开始处理【根据父级查询子级类别列表】的业务，无参数");
        List<CategoryListItemVO> list = categoryService.listByParentId(parentId);
        return JsonResult.ok(list);
    }
}
